import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-26
 * Time: 9:42
 */
//LeetCode刷题调试
public class Test {

    public static void moveZeroes(int[] nums) {
        //双指针
        int i = 0;
        int j = 0;
        while( j < nums.length){
            if(nums[j] == 0){
                j++;
                continue;
            }
            if(nums[i] == 0){
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }
            i++;
            j++;
            //1.两个零挨在一起时
            //2.j运行到最后
        }
    }
    public static void main(String[] args) {
        //1,0,1
        //0 0
        //0,1,0,3,12
        //0,0,1
        int[] nums = {0,1,0,3,12};
        moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }
    public static void moveZeroes1(int[] nums) {
        //暴力解法
        int index = 0;
        int num = 0;
        for(int i = nums.length-1; i >= 0; i--){
            if(nums[i] == 0){
                index = i;
                num++;
                for(int j = index;j < nums.length-1; j++){
                    nums[j] = nums[j+1];
                }
            }

        }

        for(int i = nums.length-num;i < nums.length; i++){
            nums[i] = 0;
        }
    }
    public static void main1(String[] args) {
        int[] nums = {0,23,4,6,5,4,0,0};
        moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }
}
